﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.Coolzon.Common
{
    public static class XmlHelper
    {

        public static string GetXElementValue(this XmlNode xe, string xeName)
        {
            XmlNode xNode = xe.SelectSingleNode(xeName);
            if (xNode == null)
            {
                return null;
            }
            return xNode.InnerText.Trim();
        }
        public static string GetXAttributeValue(this XmlNode xNode, string xattrName)
        {

            XmlAttribute res = xNode.Attributes[xattrName];
            if (res == null)
            {
                return null;
            }
            return res.Value;
        }
        public static XmlAttribute SetAttribute(this XmlNode xNode, string attrName, string value)
        {
            XmlAttribute xAttribute= xNode.OwnerDocument.CreateAttribute(attrName);
            xNode.Attributes.Append(xAttribute);
            xNode.Attributes[attrName].Value = value;

            return xAttribute;
        }
        public static XmlNode AddElement(this XmlNode xNode, string elementName,string value)
        {
            XmlElement element = xNode.OwnerDocument.CreateElement(elementName);
            element.InnerText = string.IsNullOrWhiteSpace(value) ? string.Empty : value.Trim();
            xNode.AppendChild(element);

            return element;
        }
        public static void AddElement(this XmlNode xNode, params string[] elements)
        {
            if (elements != null && elements.Length > 0)
            {
                for (int i = 0; i < elements.Length; i++)
                {
                    XmlElement element = xNode.OwnerDocument.CreateElement(elements[i]);
                    xNode.AppendChild(element);
                }
            }
        }
    }
}
